package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.service.StoreReportsService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 店长报表控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/store/reports")
@CrossOrigin
public class StoreReportsController {
    
    @Autowired
    private StoreReportsService storeReportsService;
    
    /**
     * 测试接口
     */
    @GetMapping("/test")
    public Result<String> test() {
        return Result.success("店长报表API测试成功！当前时间：" + new java.util.Date());
    }
    
    /**
     * 获取店长报表数据
     */
    @GetMapping("/data")
    // @RequirePermission("store:reports:view") // 暂时注释权限检查
    public Result<Map<String, Object>> getStoreReportsData(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "month") String timeType) {
        try {
            log.info("获取店长报表数据，参数：startDate={}, endDate={}, timeType={}", startDate, endDate, timeType);
            return storeReportsService.getStoreReportsData(startDate, endDate, timeType);
        } catch (Exception e) {
            log.error("获取店长报表数据失败", e);
            return Result.error("获取报表数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取购买统计数据
     */
    @GetMapping("/purchase-statistics")
    // @RequirePermission("store:reports:view")
    public Result<Map<String, Object>> getPurchaseStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            log.info("获取购买统计数据，参数：startDate={}, endDate={}", startDate, endDate);
            return storeReportsService.getPurchaseStatistics(startDate, endDate);
        } catch (Exception e) {
            log.error("获取购买统计数据失败", e);
            return Result.error("获取购买统计数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取收入趋势数据
     */
    @GetMapping("/revenue-trend")
    // @RequirePermission("store:reports:view")
    public Result<Map<String, Object>> getRevenueTrend(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "month") String timeType) {
        try {
            log.info("获取收入趋势数据，参数：startDate={}, endDate={}, timeType={}", startDate, endDate, timeType);
            return storeReportsService.getRevenueTrend(startDate, endDate, timeType);
        } catch (Exception e) {
            log.error("获取收入趋势数据失败", e);
            return Result.error("获取收入趋势数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取商品销量排行
     */
    @GetMapping("/product-sales-ranking")
    // @RequirePermission("store:reports:view")
    public Result<Map<String, Object>> getProductSalesRanking(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "10") int limit) {
        try {
            log.info("获取商品销量排行，参数：startDate={}, endDate={}, limit={}", startDate, endDate, limit);
            return storeReportsService.getProductSalesRanking(startDate, endDate, limit);
        } catch (Exception e) {
            log.error("获取商品销量排行失败", e);
            return Result.error("获取商品销量排行失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取客户分析数据
     */
    @GetMapping("/customer-analysis")
    // @RequirePermission("store:reports:view")
    public Result<Map<String, Object>> getCustomerAnalysis(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            log.info("获取客户分析数据，参数：startDate={}, endDate={}", startDate, endDate);
            return storeReportsService.getCustomerAnalysis(startDate, endDate);
        } catch (Exception e) {
            log.error("获取客户分析数据失败", e);
            return Result.error("获取客户分析数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 导出报表数据
     */
    @GetMapping("/export")
    // @RequirePermission("store:reports:export")
    public void exportStoreReportsData(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            HttpServletResponse response) {
        try {
            log.info("导出店长报表数据，参数：startDate={}, endDate={}", startDate, endDate);
            storeReportsService.exportStoreReportsData(startDate, endDate, response);
        } catch (Exception e) {
            log.error("导出店长报表数据失败", e);
            // 设置错误响应
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
} 